package com.book.activeMQ.consumers;

import com.book.dto.BookDto;
import com.book.service.BookService;
import com.book.util.EhCacheManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.List;

/**
 * @Author 李锦程
 * @Date 2021/4/20 9:31 下午
 * @Package com.book.activeMQ.consumers
 * @Version 1.0
 */
@Slf4j
@Component
public class UpdateCache implements MessageListener {
    @Autowired
    private BookService uBookService;
    @Autowired
    private EhCacheManager ehCacheManager;
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            ehCacheManager.evict("myCache","bookList");
            List<BookDto> uBookList = uBookService.getUBookList();
            ehCacheManager.put("myCache", "bookList", uBookList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
